﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EShop.Common;
using EShop.BLL;
using EShop.Model;
//using EShop.Common;

namespace EShop.Client
{
    public partial class frmMemberLevelManager : frmBase
    {
        public frmMemberLevelManager()
        {
            InitializeComponent();
        }

        private bool isAppend = true;

        private string strLevelID = string.Empty;

        private MemberManagerBLL oMemberManagerBLL = new MemberManagerBLL();

        private void frmMemberLevelManager_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            listLevel.Items.Clear();
            DataTable dt = oMemberManagerBLL.GetLevelInfo();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                MemberLevelInfo info = new MemberLevelInfo(
                    dt.Rows[i]["LevelID"].ToString(),
                    dt.Rows[i]["LevelName"].ToString(),
                    Convert.ToDecimal(dt.Rows[i]["LevelDiscount"])
                    );
                listLevel.Items.Add(info);
                
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            lblMsg.Visible = false;
            string strLevelName = SystemLibrary.SQLQuoteName(txtLevelName.Text);
            string strLevelDiscount = txtLevelDiscount.Text;
            if (string.IsNullOrEmpty(strLevelName))
            {
                lblMsg.Text = "等级名称不能为空";
                lblMsg.Visible = true;
                this.txtLevelName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(strLevelDiscount))
            {
                lblMsg.Text = "享受折扣不能为空";
                lblMsg.Visible = true;
                this.txtLevelDiscount.Focus();
                return;
            }
            if (isAppend)
            {
                if (oMemberManagerBLL.MemberLevelInfoAppend(strLevelName, Convert.ToDecimal(strLevelDiscount)))
                {
                    MessageBox.Show("添加成功");
                    Init(); btnReset_Click(sender, e);
                }
            }
            else
            {
                if (oMemberManagerBLL.MemberLevelInfoUpdate(strLevelID, strLevelName, Convert.ToDecimal(strLevelDiscount)))
                {
                    MessageBox.Show("修改成功");
                    Init();
                    btnReset_Click(sender, e);
                }
            }

        }

        private void lblLevelDiscount_TextChanged(object sender, EventArgs e)
        {
            try
            {
                decimal i = Convert.ToDecimal(txtLevelDiscount.Text);
                //if (i > 1) txtLevelDiscount.Text = "1";
                //if (i <= 0.1m) txtLevelDiscount.Text = "0.10";
                txtLevelDiscount.Text = Common.FormatManager.FormatDecimal(i);
            }
            catch
            {
                txtLevelDiscount.Text = "1.00";
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.txtLevelName.Clear();
            this.txtLevelDiscount.Clear();
            this.isAppend = true;
            this.btnSubmit.Text = "添加新等级";
        }

        private void listLevel_DoubleClick(object sender, EventArgs e)
        {
            int index = listLevel.SelectedIndex;
            if (index == -1) return;

            MemberLevelInfo info = (MemberLevelInfo)listLevel.Items[index];
            this.txtLevelName.Text = info.LevelName;
            this.txtLevelDiscount.Text = Common.FormatManager.FormatDecimal(info.LevelDiscount);
            this.strLevelID = info.LevelID;
            this.isAppend = false;
            this.btnSubmit.Text = "确定修改";
        }
    }
}
